'''
Created on 20/03/2009

@author: eh01
'''
from xml.dom.minidom import parseString
from im.ds.dst import DST

class DSO(object):
    '''
    classdocs
    '''

    __dsts = None
    __opening_tag = "<"
    __closing_tag = "</"
    __end_tag = ">"
    __dso_tag_name = "dso" 
    __dst_tag_name = "dst"
    
    def __init__(self, dsts = None, xml_string = None):
        if xml_string is None:
            self.__dsts = dsts
        else:
            xml = parseString(xml_string)
            dstElements = xml.getElementsByTagName(self.__dst_tag_name)
            self.__dsts = []
            for dstElement in dstElements:
                self.__dsts.append(DST(xml_string = dstElement.toxml()))
                
    def get_dsts(self, subject=None, predicate=None):
        if not subject and not predicate:
            return self.__dsts
        else:
            dsts = []
            for dst in self.__dsts:
                if dst.get_subject() == subject and dst.get_predicate() == predicate:
                    dsts.append(dst)
            return dsts

    def set_dsts(self, value):
        self.__dsts = value

    def to_xml(self):
        xml = self.__opening_tag + self.__dso_tag_name + self.__end_tag
        for dst in self.get_dsts():
            xml += dst.to_xml()
        xml += self.__closing_tag + self.__dso_tag_name + self.__end_tag
        return xml
    
    def get_dst(self, subject, predicate):
        for dst in self.__dsts:
            if dst.get_subject() == subject and dst.get_predicate() == predicate:
                return dst
        return None
    
    def remove_dst(self, dst=None, subject=None, predicate=None):
        if dst:
            self.__dsts.remove(dst)
        elif subject and predicate:
            dst = self.get_dst(subject, predicate)
            self.__dsts.remove(dst)            
        else:
            return None
        
    def add_dst(self, dst):
        self.__dsts.append(dst)
    
    def add_dsts(self, dsts):
        self.__dsts.extend(dsts)
    dsts = property(get_dsts, set_dsts, None, None)     